using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

namespace RacerGame
{
	internal abstract class FactionAI : Faction
	{
		protected float attackModifier = 0.0001f;
		protected float attack = 0;
		protected float defenseModifier = 0.0002f;
		protected float defense = 0f;
		protected float economyModifier = 0.0004f;
		protected float economy = 1.0f;
		protected Dictionary<FactionColor, float> anger = new Dictionary<FactionColor, float>();
		public float Attack { get { return attack; } }
		public float Defense { get { return defense; } }
		public float Economy { get { return economy; } }

		protected FactionAI() : base() { init(); }
		protected FactionAI(FactionColor color, Vector3 hivePosition) : base(color, hivePosition) { init(); }

		private void init() {
			anger[FactionColor.Red] = 0f;
			anger[FactionColor.Blue] = 0f;
			anger[FactionColor.Yellow] = 0f;
			anger[FactionColor.Green] = 0f;
		}

		protected override void think() {
			attack += attackModifier;
			defense += defenseModifier;
			if( Credits < 10000 ) {
				economy += economyModifier;
			}			
		}

		public override void DamageReport(FactionEntity target, FactionEntity attacker, float damage) {
			if( attacker.Faction.FactionColor != FactionColor.None ) {
				anger[attacker.Faction.FactionColor] += 0.0001f * damage;
				if( target is Structure ) {
					defense += 0.001f * damage;
				} else if( target is Unit ) {
					attack += 0.0001f * damage;
				}
			}
		}

		public override void Dead(FactionEntity target, FactionEntity attacker) {
			if( attacker.Faction == Faction.None ) return;
			anger[attacker.Faction.FactionColor] += 0.01f;
			if( target is Structure ) {
				defense += 0.3f;
			} else if( target is Unit ) {
				attack += 0.1f;
			}
		}

		protected void realAI() {
			if( Hive.Queued > 1 ) return;
			// attack
			if( attack >= economy && attack >= defense ) {
				Hive.Produce(new Scorpion(Vector3.Zero));
				attack -= 0.2f;
				if( attack < 0 ) attack = 0;
			}
			// defense
			else if( defense >= economy ) {
				Map.Cell freeCell = RacerGame.CurMap.GetFreeCell(Hive.Position, 15);
				hive.Produce(new Turret(new Vector3(freeCell.X * RacerGame.CurMap.CellSize, 0, freeCell.Z * RacerGame.CurMap.CellSize)));
				defense -= 0.5f;
				if( defense < 0 ) defense = 0;
			}
			// economy
			else {
				hive.Produce(new DataTrain(Vector3.Zero));
				economy -= 0.3f;
				if( economy < 0 ) economy = 0;
			}

			// count army
			/*int turrets = 0;
			int scorpions = 0;
			foreach( FactionEntity entity in factionEntities ) {
				if( entity.Dead ) continue;
				if( entity is Turret ) {
					turrets++;
				} else if( entity is Scorpion ) {
					scorpions++;
				}
			}
			// test			
			if( turrets < 5 ) {

			}*/
		}

		protected void simpleAI() {
			if( Hive.Queued > 2 ) return;			
			if( Credits >= DataTrain._ProductionCost && Credits > 0 && DataTrains.Count < 10 ) {
				hive.Produce(new DataTrain(Vector3.Zero));
			}
			if( Credits >= Turret._ProductionCost ) {
				Vector3 position = hive.Position;
				foreach( Faction faction in Faction.Factions ) {
					if( faction == Faction.None ) continue;
					foreach( FactionEntity fe in faction.FactionEntities ) {
						if( fe.Target == Hive ) {
							position = fe.Position;
						}
						if( position == hive.Position ) {
							position = fe.Position;
						}
					}
				}
				Map.Cell freeCell = RacerGame.CurMap.GetFreeCell(Hive.Position, 15);
				hive.Produce(new Turret(new Vector3(freeCell.X * RacerGame.CurMap.CellSize, 0, freeCell.Z * RacerGame.CurMap.CellSize)));
			}
			if( Credits >= Scorpion._ProductionCost && Credits > 500 ) {
				Hive.Produce(new Scorpion(Vector3.Zero));
			}
		}
	}
}
